<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="学号" prop="studentNo">
        <el-input
          v-model="queryParams.studentNo"
          placeholder="请输入学号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="学生姓名" prop="studentName">
        <el-input
          v-model="queryParams.studentName"
          placeholder="请输入学生姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="cellNo">
        <el-input
          v-model="queryParams.cellNo"
          placeholder="请输入手机号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单编号" prop="orderNo">
        <el-input
          v-model="queryParams.orderNo"
          placeholder="请输入订单编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单状态" prop="orderStatus">
        <el-select v-model="queryParams.orderStatus" placeholder="请选择订单状态" clearable size="small">
          <el-option
                v-for="dict in orderStatusOptions"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              />
        </el-select>
      </el-form-item>
      <el-form-item label="售后状态" prop="aftersaleStatus">
        <el-select v-model="queryParams.aftersaleStatus" placeholder="请选择售后状态" clearable size="small">
          <el-option
                v-for="dict in aftersaleStatusOptions"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!--
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['mall:order:add']"
        >新增</el-button>
      </el-col>
      -->
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['mall:order:edit']"
        >退款</el-button>
      </el-col>
      <!--
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['mall:order:remove']"
        >删除</el-button>
      </el-col>
      -->
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['mall:order:export']"
        >导出</el-button>
      </el-col>
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="orderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column type="expand">
        <template slot-scope="scope">
          <el-form label-position="left" inline>
            <div v-for="(item, index) in scope.row.mallOrderGoodsList" :key="index" class="demo-table-expand">
              <el-form-item label="商品名称">
                <span>{{ item.goodsName }}</span>
              </el-form-item>
            </div>
          </el-form>
        </template>
      </el-table-column>
      <!--<el-table-column label="订单ID" align="center" prop="orderId" />-->
      <el-table-column label="学生" align="center" prop="studentName" />
      <el-table-column label="订单编号" align="center" prop="orderNo" />
      <el-table-column label="订单状态" :formatter="orderStatusFormat" align="center" prop="orderStatus" />
      <el-table-column label="售后状态" :formatter="aftersaleStatusFormat" align="center" prop="aftersaleStatus" />
      <el-table-column label="收货人名称" align="center" prop="consignee" />
      <el-table-column label="收货人手机号" align="center" prop="mobile" />
      <el-table-column label="收货地址" align="center" prop="address" />
      <el-table-column label="用户订单留言" align="center" prop="message" />
      <el-table-column label="商品总费用" align="center" prop="goodsPrice" />
      <el-table-column label="配送费用" align="center" prop="freightPrice" />
      <el-table-column label="优惠券减免" align="center" prop="couponPrice" />
      <el-table-column label="用户积分减免" align="center" prop="integralPrice" />
      <el-table-column label="团购优惠价减免" align="center" prop="grouponPrice" />
      <el-table-column label="折扣减免" align="center" prop="discountPrice" />
      <el-table-column label="订单费用" align="center" prop="orderPrice" />
      <el-table-column label="实付费用" align="center" prop="actualPrice" />
      <el-table-column label="微信付款编号" align="center" prop="payId" />
      <el-table-column label="微信付款时间" align="center" prop="payTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.payTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="发货编号" align="center" prop="shipNo" />
      <el-table-column label="发货快递公司" align="center" prop="shipChannel" />
      <el-table-column label="发货开始时间" align="center" prop="shipTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.shipTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="实际退款金额" align="center" prop="refundAmount" />
      <el-table-column label="退款方式" :formatter="refundTypeFormat" align="center" prop="refundType" />
      <el-table-column label="退款备注" align="center" prop="refundContent" />
      <el-table-column label="退款时间" align="center" prop="refundTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.refundTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="用户确认收货时间" align="center" prop="confirmTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.confirmTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="待评价订单商品数量" align="center" prop="comments" />
      <el-table-column label="状态" :formatter="statusFormat" align="center" prop="status" />
      <el-table-column label="操作" fixed="right" min-width="120px" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['mall:order:edit']"
          >退费</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['mall:order:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改订单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="用户" prop="studentName">
              <el-input disabled v-model="form.studentName" placeholder="请输入用户" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="订单编号" prop="orderNo">
              <el-input disabled v-model="form.orderNo" placeholder="请输入订单编号" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="订单状态">
              <el-select disabled v-model="form.orderStatus" placeholder="请选择订单状态" clearable size="small">
                <el-option
                      v-for="dict in orderStatusOptions"
                      :key="dict.dictValue"
                      :label="dict.dictLabel"
                      :value="dict.dictValue"
                    />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="售后状态">
              <el-select v-model="form.aftersaleStatus" placeholder="请选择售后状态" clearable size="small">
              <el-option
                    v-for="dict in aftersaleStatusOptions"
                    :key="dict.dictValue"
                    :label="dict.dictLabel"
                    :value="dict.dictValue"
                  />
            </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="商品总费用" prop="goodsPrice">
              <el-input disabled v-model="form.goodsPrice" placeholder="请输入商品总费用" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item disabled label="配送费用" prop="freightPrice">
              <el-input disabled v-model="form.freightPrice" placeholder="请输入配送费用" />
            </el-form-item>
          </el-col>
        </el-row>
        <!--
        <el-row>
          <el-col :span="12">
            <el-form-item label="优惠券减免" prop="couponPrice">
              <el-input disabled v-model="form.couponPrice" placeholder="请输入优惠券减免" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="用户积分减免" prop="integralPrice">
              <el-input disabled v-model="form.integralPrice" placeholder="请输入用户积分减免" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="团购优惠价减免" prop="grouponPrice">
              <el-input disabled v-model="form.grouponPrice" placeholder="请输入团购优惠价减免" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="折扣减免" prop="discountPrice">
              <el-input disabled v-model="form.discountPrice" placeholder="请输入折扣减免" />
            </el-form-item>
          </el-col>
        </el-row>
        -->
        <el-row>
          <el-col :span="12">
            <el-form-item label="订单费用" prop="orderPrice">
              <el-input disabled v-model="form.orderPrice" placeholder="请输入订单费用" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="实付费用" prop="actualPrice">
              <el-input disabled v-model="form.actualPrice" placeholder="请输入实付费用" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="付款时间" prop="payTime">
              <el-date-picker disabled clearable size="small" style="width: 200px"
                v-model="form.payTime"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="选择微信付款时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="退款金额" prop="refundAmount">
              <el-input v-model="form.refundAmount" placeholder="请输入退款金额" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="退款方式" prop="refundType">
            <el-select v-model="form.refundType" placeholder="请选择状态">
                    <el-option
                        v-for="dict in refundTypeOptions"
                        :key="dict.dictValue"
                        :label="dict.dictLabel"
                        :value="dict.dictValue"
                      />
          </el-select>  
        </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="退款备注">
              <editor v-model="form.refundContent" :min-height="192"/>
            </el-form-item>
          </el-col>
        </el-row>

        <!--
        <el-form-item label="状态">
          <el-select v-model="form.status" placeholder="请选择状态">
                    <el-option
                        v-for="dict in statusOptions"
                        :key="dict.dictValue"
                        :label="dict.dictLabel"
                        :value="dict.dictValue"
                      />
          </el-select>
        </el-form-item>
        -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listOrder, getOrder, delOrder, addOrder, updateOrder, exportOrder } from "@/api/mall/order";
import Editor from '@/components/Editor';

export default {
  name: "Order",
  components: { Editor },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单表格数据
      orderList: [],
      statusOptions: [],
      orderStatusOptions: [],
      aftersaleStatusOptions: [],
      refundTypeOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        studentNo: null,
        studentName: null,
        cellNo: null,
        orderNo: null,
        orderStatus: null,
        aftersaleStatus: null,
        consignee: null,
        mobile: null,
        address: null,
        message: null,
        goodsPrice: null,
        freightPrice: null,
        couponPrice: null,
        integralPrice: null,
        grouponPrice: null,
        discountPrice: null,
        orderPrice: null,
        actualPrice: null,
        payId: null,
        payTime: null,
        shipNo: null,
        shipChannel: null,
        shipTime: null,
        refundAmount: null,
        refundType: null,
        refundContent: null,
        refundTime: null,
        confirmTime: null,
        comments: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        /*
        userId: [
          { required: true, message: "用户ID不能为空", trigger: "blur" }
        ],
        goodsPrice: [
          { required: true, message: "商品总费用不能为空", trigger: "blur" }
        ],
        freightPrice: [
          { required: true, message: "配送费用不能为空", trigger: "blur" }
        ],
        couponPrice: [
          { required: true, message: "优惠券减免不能为空", trigger: "blur" }
        ],
        integralPrice: [
          { required: true, message: "用户积分减免不能为空", trigger: "blur" }
        ],
        grouponPrice: [
          { required: true, message: "团购优惠价减免不能为空", trigger: "blur" }
        ],
        discountPrice: [
          { required: true, message: "折扣减免不能为空", trigger: "blur" }
        ],
        orderPrice: [
          { required: true, message: "订单费用不能为空", trigger: "blur" }
        ],
        actualPrice: [
          { required: true, message: "实付费用不能为空", trigger: "blur" }
        ],
        */
      }
    };
  },
  created() {
    this.getDicts("sys_normal_disable").then(response => {
      this.statusOptions = response.data;
    });
    this.getDicts("mall_order_status").then(response => {
      this.orderStatusOptions = response.data;
    });
    this.getDicts("mall_after_sale_status").then(response => {
      this.aftersaleStatusOptions = response.data;
    });
    this.getDicts("mall_refund_type").then(response => {
      this.refundTypeOptions = response.data;
    });

    this.getList();
  },
  methods: {
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    orderStatusFormat(row, column) {
      return this.selectDictLabel(this.orderStatusOptions, row.orderStatus);
    },
    aftersaleStatusFormat(row, column) {
      return this.selectDictLabel(this.aftersaleStatusOptions, row.aftersaleStatus);
    },
    refundTypeFormat(row, column) {
      return this.selectDictLabel(this.refundTypeOptions, row.refundType);
    },
    /** 查询订单列表 */
    getList() {
      this.loading = true;
      listOrder(this.queryParams).then(response => {
        this.orderList = response.rows;
        console.log(JSON.stringify(this.orderList))
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        orderId: null,
        userId: null,
        studentName: null,
        orderNo: null,
        orderStatus: "0",
        aftersaleStatus: "0",
        consignee: null,
        mobile: null,
        address: null,
        message: null,
        goodsPrice: null,
        freightPrice: null,
        couponPrice: null,
        integralPrice: null,
        grouponPrice: null,
        discountPrice: null,
        orderPrice: null,
        actualPrice: null,
        payId: null,
        payTime: null,
        shipNo: null,
        shipChannel: null,
        shipTime: null,
        refundAmount: null,
        refundType: null,
        refundContent: null,
        refundTime: null,
        confirmTime: null,
        comments: null,
        status: "0",
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        mallOrderGoodsList: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.orderId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加订单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const orderId = row.orderId || this.ids
      getOrder(orderId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "退费管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.orderId != null) {
            updateOrder(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addOrder(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const orderIds = row.orderId || this.ids;
      this.$confirm('是否确认删除订单编号为"' + orderIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delOrder(orderIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有订单数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportOrder(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
<style scoped>
  .demo-table-expand {
    font-size: 0;
    margin-left: 50px;
  }
  .demo-table-expand label {
    width: 90px;
    color: #8e98a7;
    color: #b1b4b8;
  }
  .demo-table-expand .el-form-item {
    margin-right: 0;
    margin-bottom: 0;
    width: 50%;
  }
</style>